package com.heo.music.player.config.swagger;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.ParameterBuilderPlugin;
import springfox.documentation.spi.service.contexts.ParameterContext;
import springfox.documentation.swagger.common.SwaggerPluginSupport;

/**
 * @author 星天无极
 * @create 2022-05-19 10:20
 **/
@Component
@Slf4j
@ConditionalOnProperty(
    name = {"knife4j.enable"},
    havingValue = "true"
)
public class CustomParameterBuilderPlugin implements ParameterBuilderPlugin {

    private static final String PATH = "path";

    @Override
    public void apply(ParameterContext parameterContext) {
        String paramType = parameterContext.parameterBuilder().build().getParamType();
        if (PATH.equals(paramType)) {
            parameterContext.parameterBuilder().required(true).order(Integer.MIN_VALUE);
        }
    }

    @Override
    public boolean supports(@NonNull DocumentationType delimiter) {
        return SwaggerPluginSupport.pluginDoesApply(delimiter);
    }
}
